#!/usr/bin/env bash

path=`pwd`
export GOPATH=$path

libpath="src/code.google.com/p/weed-fs/go"

if [ ! -d "bin" ]; then
	mkdir bin
fi

if [ ! -d "src" ]; then
mkdir src
fi

if [ -f "bin/weed" ]; then
	rm -rf bin/weed
fi

if [ ! -d $libpath ]; then
	mkdir -p $libpath
	mkdir $libpath/weed
	ln -s $path/go/filer $path/$libpath/filer
	ln -s $path/go/glog $path/$libpath/glog
	ln -s $path/go/operation $path/$libpath/operation
	ln -s $path/go/proto $path/$libpath/proto
	ln -s $path/go/sequence $path/$libpath/sequence
	ln -s $path/go/stats $path/$libpath/stats
	ln -s $path/go/storage $path/$libpath/storage
	ln -s $path/go/topology $path/$libpath/topology
	ln -s $path/go/util $path/$libpath/util
	ln -s $path/go/weed/weed_server $path/$libpath/weed/weed_server
	ln -s $path/go/weed $path/src/weed
fi



if [ ! -d $path/src/code.google.com/p/goprotobuf/proto ]; then
	echo "get code.google.com/p/goprotobuf/proto"
	go get code.google.com/p/goprotobuf/proto
fi

if [ ! -d $path/src/code.google.com/p/gogoprotobuf/proto ]; then
	echo "get code.google.com/p/gogoprotobuf/proto"
	go get code.google.com/p/gogoprotobuf/proto
fi

if [ ! -d $path/src/github.com/gorilla/mux ]; then
	echo "get github.com/gorilla/mux"
	go get github.com/gorilla/mux
fi

if [ ! -d $path/src/github.com/tgulacsi/go-cdb ]; then
	echo "get github.com/tgulacsi/go-cdb"
	go get github.com/tgulacsi/go-cdb
fi

if [ ! -d $path/src/code.google.com/p/snappy-go/snappy ]; then
	echo "get code.google.com/p/snappy-go/snappy"
	go get code.google.com/p/snappy-go/snappy
fi

if [ ! -d $path/src/github.com/syndtr/goleveldb/leveldb ]; then
	echo "get github.com/syndtr/goleveldb/leveldb"
	go get github.com/syndtr/goleveldb/leveldb
fi

if [ ! -d $path/src/github.com/goraft/raft ]; then
	echo "get github.com/goraft/raft"
	go get github.com/goraft/raft
fi

echo "build weed" 
go build -o bin/weed weed
